package cn.cvs.web.converter;

import org.apache.log4j.Logger;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class String2DateConverter implements Converter<String, Date> {
    private Logger logger = Logger.getLogger(String2DateConverter.class);
    private String datePattern;

    public String2DateConverter(String datePattern) {
        this.logger.info("加载String2DateConverter");
        this.datePattern = datePattern;
    }

    public Date convert(String s) {
        Date date = null;

        try {
            date = (new SimpleDateFormat(this.datePattern)).parse(s);
            this.logger.info("String2DateConverter convert date:" + date);
        } catch (ParseException var4) {
            this.logger.error("日期转换失败：" + s);
            var4.printStackTrace();
        }

        return date;
    }
}
